package com.kong.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.kong.constant.AgeEnum;
import com.kong.constant.SexEnum;
import com.kong.dao.MemberDao;
import com.kong.pojo.Member;
import com.kong.service.MemberService;
import com.kong.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

import static com.kong.constant.AgeEnum.*;

/**
 * @author yang2013
 * @data 2020/10/15 20:29
 */
@Service
public class MemberServiceImpl implements MemberService {
    private static int start=6;
    private static int end=14;
    @Autowired
    private MemberDao memberDao;
    @Override
    public Member findByPhone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member1) {
        memberDao.add(member1);
    }

    @Override
    public Map getMemberReport() {
        Map map=new HashMap();
        //获得当前月份以及前一年所有的月份
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-12);

        List<String> months = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            calendar.add(Calendar.MONTH,1);
            months.add(new SimpleDateFormat("yyyy-MM").format(calendar.getTime()));
        }
        map.put("months",months);
        List<Integer> memberCount = new ArrayList<>();
        //查找每个月之前的会员总数  mybatis传入string类型
        for (String s : months) {
            String date=s+"-31";
            Integer memberCountBeforeDate = memberDao.findMemberCountBeforeDate(date);
            memberCount.add(memberCountBeforeDate);
        }
        map.put("memberCount",memberCount);

        return map;

    }

    /**
     * 通过性别的饼图
     * @return
     * 更改为使用lamada表达式
     */
    @Override
    public Map getMemberReportBySex() {
        Map map=new HashMap();
        List<String> name =new ArrayList<String>(){{
                add(SexEnum.MAN.getSex());
                add(SexEnum.WOMAN.getSex());
                add(SexEnum.UN_KNOW.getSex());
            }
        };
        List<Long> count=new ArrayList<Long>();
        List<Member> allMember = memberDao.findAll();
        for (String s : name) {
            long num = allMember.stream().
                    filter(member -> member.getSex().equals(SexEnum.sexOf(s).getSex())).
                    count();
            count.add(num);

        }
        List<Map> memberCount =setMemberCount(name,count);
        map.put("memberName",name);
        map.put("memberCount",memberCount);
        return map;
    }


    /**
     * 组装List<map>的公共类
     * @param names
     * @param count
     * @return
     */
    private List<Map> setMemberCount(List<String> names, List count) {
        List<Map> mapList=new ArrayList<>();
        for (int i = 0; i < names.size(); i++) {
            Map map=new HashMap();
            map.put("name",names.get(i));
            map.put("value",count.get(i));
            mapList.add(map);
        }
        return mapList;
    }


    public static void main(String[] args) {
        String str=null;
        System.out.println(str.equals(null));
    }
    /**
     * 通过年龄的饼图
     * @return
     */
    @Override
    public Map getMemberReportByAge() throws Exception {
        Map map = new HashMap();
        List<Member> all = memberDao.findAll();
        List<Integer> integerList=new ArrayList<>();
        //将所有的年龄加入集合---通过MySQL日期函数计算年龄
        for (Member member : all) {
            if (!member.getIdCard().equals(String.valueOf(UN_KNOWN.getAge()))){
                Date date = DateUtils.parseString2Date(member.getIdCard().substring(start, end),"yyyyMMdd");
                int age = DateUtils.getAge(date);
                integerList.add(age);
            }else {
                integerList.add(UN_KNOWN.getAge());
            }

        }
        List<String> memberName = new ArrayList<>();
        //0-18
        List<Long> count = new ArrayList<>();
        long minor = integerList.stream().
                filter(integer -> integer >= AgeEnum.MINOR_LOWER.getAge()).
                filter(integer -> integer <= AgeEnum.MINOR_HIGH.getAge()).count();
        memberName.add(MINOR_LOWER.getName());
        count.add(minor);
        //18-30
        long young = integerList.stream().
                filter(integer -> integer >= YOUNG_LOWER.getAge()).
                filter(integer -> integer <= AgeEnum.YOUNG_HIGH.getAge()).count();
        memberName.add(YOUNG_LOWER.getName());
        count.add(young);
        //30-45
        long middle = integerList.stream().
                filter(integer -> integer >= MIDDLE_AGE_LOW.getAge()).
                filter(integer -> integer <= AgeEnum.MIDDLE_AGE_HIGH.getAge()).count();
        memberName.add(MIDDLE_AGE_LOW.getName());
        count.add(middle);
        //45
        long old = integerList.stream().
                filter(integer -> integer >= AgeEnum.OLD_AGE.getAge()).count();
        memberName.add(OLD_AGE.getName());
        count.add(old);
        //未知
        long unKnown = integerList.stream().
                filter(integer -> integer.equals(UN_KNOWN.getAge())).count();
        memberName.add(UN_KNOWN.getName());
        count.add(unKnown);

        List<Map> memberCount = setMemberCount(memberName, count);
        map.put("memberCount",memberCount);
        map.put("memberName",memberName);
        return map;
    }

    /**
     * 获取指定日期时间段内的会员数量 拓展--如果只有一个月就拆分为天
     * @return
     */
    @Override
    public Map getMemberReportByDate(String startDate,String endDate) throws Exception {
        Map map=new HashMap();
        //获得月份列表
        List<String> months = DateUtils.getMonthBetween(startDate, endDate, "yyyy-MM");
        map.put("months",months);
        List<Integer> memberCount = new ArrayList<>();
        //查找每个月之前的会员总数  mybatis传入string类型
        for (String s : months) {
            String date=s+"-31";
            Integer memberCountBeforeDate = memberDao.findMemberCountBeforeDate(date);
            memberCount.add(memberCountBeforeDate);
        }
        map.put("memberCount",memberCount);
        return map;
    }
}
